#ifndef LISTACIRCULAR_HPP_
#define LISTACIRCULAR_HPP_

#include "container.hpp"


template<class T>
class listaCircular : public container<T>
{
public:
	listaCircular();
	
	~listaCircular();
	
	void
	push(const T);
	
	void
	pop();
	
	void
	next();
	
	void
	previous();
};

/****************************
 * 
 *         Implementations
 * 
 * *************************/



template<class T>
listaCircular<T>::listaCircular()
{
	this->cursor = 0;
}

template<class T>
listaCircular<T>::~listaCircular()
{

}

template<class T> void
listaCircular<T>::push( const T _info)
{
	noh<T>*
	newNoh = new noh<T>(_info);

	if (this->cursor == 0)
	{
		this->cursor = newNoh;
		
		this->cursor->setPosterior( this->cursor);
		this->cursor->setAnterior( this->cursor);
	}
	else 
	{
		noh<T>*
		nohAnterior = this->cursor->getAnterior();
		
		nohAnterior->setPosterior(newNoh);
		
		newNoh->setAnterior(nohAnterior);
		newNoh->setPosterior(this->cursor);
		
		this->cursor->setAnterior(newNoh);
		
		this->cursor = newNoh;
	}
}

template<class T> void
listaCircular<T>::pop()
{
	if (this->cursor != 0 )
	{
		if(this->cursor == this->cursor->getProximo())
		{
			delete this->cursor;
			this->cursor = 0;
		}
		else
		{
			noh<T>*
			nohAnterior = this->cursor->getAnterior();
			
			noh<T>*
			nohPosterior = this->cursor->getPosterior();
			
			nohAnterior->setProximo( nohPosterior );
			nohPosterior->setAnterior( nohAnterior );
			
			delete this->cursor;
			
			this->cursor = nohPosterior;
		}
		
		
	}
	
}

template<class T> void
listaCircular<T>::next()
{
	this->cursor = this->cursor->getPosterior();
}

template<class T> void
listaCircular<T>::previous()
{
	this->cursor = this->cursor->getAnterior();
}

#endif /*LISTACIRCULAR_HPP_*/
